<?
require_once(dirname(dirname(dirname(__FILE__))) . "/_includes/config.php");
require_once(dirname(dirname(dirname(__FILE__))) . "/_includes/classes/paginacao.class.php");


$WHERE = '';
if ($_REQUEST['form_enviado'] == 1){
	$dc_cupom = $_REQUEST['dc_cupom'];
	$WHERE = "WHERE c.dc_cupom = '$dc_cupom'";
}
?>

<h2>1 | Selecione o Cupom</h2>
<form name="form1" method="post" action="">
  <div class="formArea">

<table border="0" cellpadding="0" cellspacing="0" class="formCampos">
  <tr>
    <td class="formLabel">Cupom:</td>
    <td>
		<input name="dc_cupom" type="text" class="formInput formMedio" id="dc_cupom" value="<?=$dc_cupom?>">
  	</td>
  </tr>
  <tr>
    <td class="formLabel">&nbsp;</td>
    <td>
		<input name="formButtonOk" type="submit" class="formButton" id="formButtonOk" value="OK">
		<input name="form_enviado" type="hidden" id="form_enviado" value="1">
	</td>
  </tr>
</table>
</form>

<?
$sql = "
SELECT
	COUNT(ci.cd_cupom_item) AS qtd_item, ci.cd_cupom,
	c.dc_cupom, DATE_FORMAT(c.dt_inicio, '%d/%m/%Y') AS dt_inicio,
	DATE_FORMAT(c.dt_fim, '%d/%m/%Y') AS dt_fim,
	IF(ci.vl_preco > 0, CONCAT('R$ ', ci.vl_preco), CONCAT(ci.vl_percentual, '%')) AS valor,
	IF(ci.vl_preco > 0, CONCAT('_', ci.vl_preco), CONCAT('__', ci.vl_percentual)) AS valor_param
	, c.dc_titulo
FROM
	cupom_item AS ci
	INNER JOIN cupom AS c
		ON ci.cd_cupom = c.cd_cupom
$WHERE
GROUP BY
	ci.cd_cupom, vl_percentual, vl_preco
ORDER BY
	c.dt_fim DESC
";

$pg = new paginacao($sql);
$pg->consulta;

$pg->cabecalho = '
  <?php $z = 0; ?>
  <tr class="linhaHeader">
    <td>Cupom</td>
	<td>Descri&ccedil;&atilde;o</td>
	<td>Data In&iacute;cio</td>
	<td>Data Fim</td>
	<td>Qtd Itens</td>
	<td>Valor</td>
  </tr>
  ';

$pg->linha = '
  <tr class="linha<?=$c&1?>" id="linha_<?php echo $z; ?>" onMouseOver="this.className=\'linhaOver\'; this.style.cursor=\'pointer\'" onMouseOut="this.className=\'linha<?=$c++&1?>\'" onClick="javascript:removerCupomItem(\'linha_<?php echo $z; $z++; ?>\', \'<?=$lst[\'cd_cupom\'];?>\', \'<?=$lst[\'valor_param\'];?>\');">
    <td><?=$lst[\'dc_cupom\']?></td>
	<td><?=$lst[\'dc_titulo\']?></td>
	<td><?=$lst[\'dt_inicio\']?></td>
	<td><?=$lst[\'dt_fim\']?></td>
	<td><?=$lst[\'qtd_item\']?></td>
	<td><?=$lst[\'valor\']?></td>
  </tr>
  ';

$pg->resultado();

?>
<script type="text/javascript">
function removerCupomItem(aLinha, aCupom, aValor) {
	if(window.confirm("Remover os itens deste cupom?")) {
		$.post(
			"ajax.php?c=remover-cupom-item",
			{ vl: aValor, cd_cupom: aCupom },
			function(r) {
				if(parseInt(r) == 1) {
					$('#' + aLinha).hide().remove();
				} else {
					window.alert("Erro ao remover este item. Tente novamente.");
				}
			}
		);
	}
}
</script>